package com.xtm.exercise.utils;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 统一返回结果对象
 * @param <T>
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResponse<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "是否成功")
    private Boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private T data;

    public static CommonResponse success(){
        CommonResponse r = new CommonResponse();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("操作成功");
        return r;
    }

    public static<T> CommonResponse success(T data){
        CommonResponse r = new CommonResponse();
        r.setSuccess(true);
        r.setCode(ResultCode.SUCCESS);
        r.setMessage("操作成功");
        r.setData(data);
        return r;
    }

    public static<T> CommonResponse success(Integer code, String message , T data){
        CommonResponse r = new CommonResponse();
        r.setSuccess(true);
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

    public static CommonResponse error(){
        CommonResponse r = new CommonResponse();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage("操作失败");
        return r;
    }

    public static CommonResponse error(String message){
        CommonResponse r = new CommonResponse();
        r.setSuccess(false);
        r.setCode(ResultCode.ERROR);
        r.setMessage(message);
        return r;
    }

    public static CommonResponse error(Integer code, String message){
        CommonResponse r = new CommonResponse();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        return r;
    }

    public static<T> CommonResponse error(Integer code, String message, T data){
        CommonResponse r = new CommonResponse();
        r.setSuccess(false);
        r.setCode(code);
        r.setMessage(message);
        r.setData(data);
        return r;
    }

}

